import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class Ex2_2 {

	// Map of codons and aminoacides
	private HashMap<String,String> hm;
	// genomes associated to peptide
	private ArrayList<String> genomesPep;

	public Ex2_2() {
		// initialize codon list
		try {
			this.hm = Ex2_2.createCodonList(
					"C:\\Users\\josemfer\\Downloads\\RNA_codon_table_1.txt");
			genomesPep = new ArrayList<String>();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public String reverse(String seq) {		
		StringBuilder sb = new StringBuilder();		
		for (int i=0;i<seq.length();i++) {
			String nuc = seq.substring(i,i+1);
			if (nuc.equals("A")) {
				sb.append("T");
			} else if (nuc.equals("C")) {
				sb.append("G");
			}  else if (nuc.equals("T")) {
				sb.append("A");
			}	else if (nuc.equals("G")) {
				sb.append("C");
			}			


		} // for
		return sb.reverse().toString();	
	}

	
	public void getGenomesPeptide(String genomeIn, String peptide) {	
		String c = String.valueOf(peptide.charAt(0));
		if (!c.equals("")) {
			for (Map.Entry<String, String> entry : this.hm.entrySet()) {
				String key = entry.getKey();
				if (c.equals(entry.getValue())) {
					if (peptide.length()>1) {							
						getGenomesPeptide(genomeIn + key,  peptide.substring(1));
					} else {
						genomesPep.add( (genomeIn + key).replaceAll("U", "T")  );
						genomesPep.add(  reverse((genomeIn + key).replaceAll("U", "T") )  );
					}					
				}						
			}
		}		
	}	

	public static HashMap<String, String> createCodonList(String file) 
			throws IOException {		
		HashMap<String, String> hm = new HashMap<String, String>();
		BufferedReader reader = new BufferedReader( new FileReader (file));
		String         line = null;				
		while( ( line = reader.readLine() ) != null ) {			
			String[] codon = line.split("\\s+");
			if (codon.length == 2) {
				hm.put(codon[0], codon[1]);
			} else if (codon.length == 1) {
				hm.put(codon[0], null);
			}					
		}		
		return hm;		
	}	

	public int numberOf(String phrase, String word) {
		int pos = -1;
		int n = 0;
		do {
			pos = phrase.indexOf(word, pos+1);
			if (pos != -1) {
				n++;
			}
		} while (pos != -1);
		return n;
	}
	

	public static void main(String[] args) {
		try {
			// input parameters
			//String gen = "ATGGCCATGGCCCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA";
			String gen = KmerCounter.readFile("C:\\Users\\josemfer\\Downloads\\B_brevis.txt").trim();			
			String pep = "VKLFPWFNQY";

			Ex2_2 exercise = new Ex2_2();
			exercise.getGenomesPeptide("",pep);
			
			for (String seq : exercise.genomesPep) {
				if (gen.contains(seq)) {
					//System.out.println(seq + " , "  + exercise.numberOf(gen,seq));
					System.out.println(seq);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
